package com.xing.binbackend.pattern.observer31;


import java.util.ArrayList;
import java.util.List;

// 2. 具体主题实现
public class OrderStatusManager implements OrderSubject {
    private final List<OrderObserver> observers = new ArrayList<>();

    public void updateOrderStatus(Order order, String newStatus) {
        order.setStatus(newStatus);
        notifyObservers(order);
    }

    @Override
    public void registerObserver(OrderObserver observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(OrderObserver observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers(Order order) {
        observers.forEach(observer -> {
            try {
                observer.onOrderStatusChange(order);
            } catch (Exception e) {
                // 处理观察者异常
                System.err.println("Observer error: " + e.getMessage());
            }
        });
    }
}